package ie.dkit.semanticPhotoWeb.client.panels.photo;

import ie.dkit.semanticPhotoWeb.client.ClientServerConnectorAsync;
import ie.dkit.semanticPhotoWeb.client.dtos.CommentDTO;
import ie.dkit.semanticPhotoWeb.client.dtos.ImageDTO;
import ie.dkit.semanticPhotoWeb.client.tools.AsyncCallbackAdapter;

import java.util.List;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextArea;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;

/**
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public class CommentsPanel extends Panel {

	private class CommentDeleter extends ButtonListenerAdapter {

		private CommentDTO m_DTO;

		public CommentDeleter(CommentDTO dto) {
			m_DTO = dto;
		}

		public void onClick(Button button, EventObject e) {
			MessageBox.wait("Deleting Comment");
			m_ClientServerConnectorAsync.deleteComment(m_DTO,
					new AsyncCallbackAdapter() {

						public void onFailure(Throwable caught) {
							MessageBox.alert("Error",
									"Can't delete Comment. Reason."
											+ caught.getMessage());
						}

						public void onSuccess(Object result) {
							MessageBox.alert("Comment Deleted.");
							getComments();
						}

					});
		}
	}

	private ClientServerConnectorAsync m_ClientServerConnectorAsync;

	private ImageDTO m_ImageDTO;

	private Panel m_ActualComments;

	private TextArea m_TextArea;

	protected CommentsPanel(ClientServerConnectorAsync clientServerConnectorAsync,
			ImageDTO imageDTO) {
		m_ClientServerConnectorAsync = clientServerConnectorAsync;
		m_ImageDTO = imageDTO;
		setTitle("Comments");
		setCollapsible(true);
		setAutoScroll(true);
		setHeight(100);
		add(m_ActualComments = new Panel());
		add(new HTML("Add a new Comment:"));
		add(m_TextArea = new TextArea());
		add(new Button("Post", new ButtonListenerAdapter() {

			public void onClick(Button button, EventObject e) {
				MessageBox.wait("Submitting Comment");
				m_ClientServerConnectorAsync.addComment(m_ImageDTO,
						new CommentDTO(m_TextArea.getText()),
						new AsyncCallbackAdapter() {
							public void onFailure(Throwable caught) {
								MessageBox.alert("Error",
										"Wasn't able to comment. Reason: "
												+ caught.getMessage());
							}

							public void onSuccess(Object result) {
								MessageBox.alert("Successful commented");
								getComments();
							}
						});
			}

		}));
		getComments();
	}

	protected final void getComments() {
		m_ClientServerConnectorAsync.getComments(m_ImageDTO,
				new AsyncCallbackAdapter() {
					public void onSuccess(Object result) {
						showComments((List) result);
					}
				});
	}

	protected final void showComments(List commentsDTOs) {
		m_ActualComments.clear();
		for (int i = 0; i < commentsDTOs.size(); i++) {
			CommentDTO comment = (CommentDTO) commentsDTOs.get(i);
			m_ActualComments.add(new HTML("<p>"
					+ commentsDTOs.get(i).toString() + "</p>"));
			if (comment.editable) {
				m_ActualComments.add(new Button("delete", new CommentDeleter(
						comment)));
			}
			m_ActualComments.add(new HTML("<hr>"));
		}
		m_ActualComments.doLayout();
	}
}
